<template>
	<view class="container">
		<u-form :model="form" ref="uForm" labelWidth="0">
			<view class="form-container">
				<u-form-item prop="name">
					<view class="form-flex">
						<span class="form-label">名称<span class="label-required">*</span></span>
						<u--input v-model="form.name" placeholder="请输入名称" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item prop="room_num">
					<view class="form-flex">
						<span class="form-label">房间数<span class="label-required">*</span></span>
						<u--input v-model="form.room_num" placeholder="请输入房间数" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item prop="kt_num">
					<view class="form-flex">
						<span class="form-label">客厅数<span class="label-required">*</span></span>
						<u--input v-model="form.kt_num" placeholder="请输入客厅数" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item prop="toilet_num">
					<view class="form-flex">
						<span class="form-label">卫生间数<span class="label-required">*</span></span>
						<u--input v-model="form.toilet_num" placeholder="请输入卫生间数" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item prop="sort_num">
					<view class="form-flex">
						<span class="form-label">排序值<span class="label-required">*</span></span>
						<u--input v-model="form.sort_num" placeholder="请输入排序值" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item prop="type">
					<view class="form-flex">
						<span class="form-label">出租方式</span>
						<u-radio-group v-model="form.type" activeColor="#FE4F04" class="u-radio-group--row">
							<u-radio :name="item.key" label="item.value" v-for="item in types"> {{item.value}} </u-radio>
						</u-radio-group>
					</view>
				</u-form-item>
			</view>

			<view class="form-bottom-opt">
				<u-button @click="submit" v-show="!form.id" :custom-style="submitBtnStyle">提交</u-button>
			</view>
		</u-form>

	</view>
</template>


<script>
	import {
		setHxAdd,setHxEdit
	} from '@/api/part2.js'
	export default {
		data() {
			return {
				form: {
					name: '',
					room_num: '',
					kt_num:'',
					toilet_num:'',
					sort_num:'',
					type: '',
				},
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写名称',
						trigger: ['blur', 'change']
					},
					'room_num': {
						type: 'string',
						required: true,
						message: '请填写房间数',
						trigger: ['blur', 'change']
					},
					'kt_num': {
						type: 'string',
						required: true,
						message: '请填写客厅数',
						trigger: ['blur', 'change']
					},
					'toilet_num': {
						type: 'string',
						required: true,
						message: '请填写卫生间数',
						trigger: ['blur', 'change']
					},
					'sort_num': {
						type: 'string',
						required: true,
						message: '请填写排序值',
						trigger: ['blur', 'change']
					},
					'type': {
						type: 'string',
						required: true,
						message: '请选中出租类型',
						trigger: ['blur', 'change']
					},
				},
				
				types: [
					{ key: '1', value: '整租' },
					{ key: '2', value: '合租' },
				],
				submitBtnStyle: {
					background: "#FE4F04",
					boShadow: "0rpx 4rpx 8rpx 0rpx rgba(254,79,4,0.5)",
					borderRadius: "44rpx",
					fontSize: "30rpx",
					color: "#FFFFFF",
					height: "88rpx",
				},
			};
		},
		onLoad(options) {
			// if(options.id) {
			// 	this.form.id = options.id
			// 	this.getnoticeInfo()
			// }
			if(options.data){
				this.form = JSON.parse(options.data)
				this.form.type = Number(this.form.type) + ''
			}
		},
		methods: {
			// getnoticeInfo(){
			// 	noticeInfo(this.form.id).then(res=>{
			// 		console.log(res.data.data)
			// 		let data = res.data.data
			// 		this.form = {...data}
			// 	})
			// },

			submit() {
				console.log('提交')
				this.$refs.uForm.validate().then(res => {
					let fun = this.form.id ? setHxAdd : setHxAdd
					fun(this.form).then(res => {
						uni.$u.toast(res.msg || '添加成功')
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					})
				}).catch(errors => {
					console.log(errors)
					uni.$u.toast('校验失败')
				})
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	};
</script>

<style lang="less" scoped>
	.container {
		padding-bottom: 150rpx;
		margin-bottom: constant(safe-area-inset-bottom);
		margin-bottom: env(safe-area-inset-bottom);
		background: #F5F6FA;
		min-height: 100vh;
	}

	.form-subtitle {
		padding: 24rpx 40rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 700;
		font-size: 26rpx;
		color: #2A0C00;
		line-height: 36rpx;

		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.form-container {
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.01);
		border-radius: 16rpx;
		padding: 0 40rpx;
		margin-bottom: 20rpx;

		.form-flex {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.form-label {
			margin-right: 10rpx;

			.label-required {
				color: #FF0000;
				padding: 0 4rpx;
			}
		}
	}

	.form-bottom-opt {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 1000;
		/* 关键样式 - 使用 constant 和 env 兼容不同设备 */
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS < 11.2 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iOS >= 11.2 */
		/* 其他样式按需添加 */
		background-color: #F5F6FA;
		padding: 20rpx 40rpx;
		display: flex;
		gap: 48rpx;
	}


	/deep/ .u-form-item {
		justify-content: flex-end !important;
		/* 右对齐 */
	}

	/deep/ .u-radio-group--row {
		gap: 30rpx;
		justify-content: flex-end;
		align-items: center;
	}
</style>